package com.ruoyi.event.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.dto.EventPlan;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.event.domain.EventJcjh;
import com.ruoyi.event.domain.bo.EventJcjhBo;
import com.ruoyi.event.domain.vo.EventJcjhVo;
import com.ruoyi.event.mapper.EventJcjhMapper;
import com.ruoyi.event.service.IEventJcjhService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 检测计划Service业务层处理
 *
 * @author strongme
 * @date 2023-02-04
 */
@RequiredArgsConstructor
@Service
public class EventJcjhServiceImpl implements IEventJcjhService {

    private final EventJcjhMapper baseMapper;


    /**
     * 查询检测计划
     */
    @Override
    public EventJcjhVo queryById(Long jcjhId) {
        return baseMapper.selectVoById(jcjhId);
    }

    /**
     * 查询检测计划列表
     */
    @Override
    public TableDataInfo<EventJcjhVo> queryPageList(EventJcjhBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EventJcjh> lqw = buildQueryWrapper(bo);
        Page<EventJcjhVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询检测计划列表
     */
    @Override
    public List<EventJcjhVo> queryList(EventJcjhBo bo) {
        LambdaQueryWrapper<EventJcjh> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EventJcjh> buildQueryWrapper(EventJcjhBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EventJcjh> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, EventJcjh::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getJcr()), EventJcjh::getJcr, bo.getJcr());
        lqw.eq(StringUtils.isNotBlank(bo.getJclx()), EventJcjh::getJclx, bo.getJclx());
        lqw.eq(StringUtils.isNotBlank(bo.getJcfs()), EventJcjh::getJcfs, bo.getJcfs());
        lqw.eq(bo.getJcrq() != null, EventJcjh::getJcrq, bo.getJcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getJcjg()), EventJcjh::getJcjg, bo.getJcjg());
        lqw.eq(StringUtils.isNotBlank(bo.getJcdd()), EventJcjh::getJcdd, bo.getJcdd());
        lqw.eq(StringUtils.isNotBlank(bo.getJcbg()), EventJcjh::getJcbg, bo.getJcbg());
        lqw.eq(StringUtils.isNotBlank(bo.getSp()), EventJcjh::getSp, bo.getSp());
        lqw.eq(StringUtils.isNotBlank(bo.getFj()), EventJcjh::getFj, bo.getFj());
        lqw.eq(bo.getLat() != null, EventJcjh::getLat, bo.getLat());
        lqw.eq(bo.getLng() != null, EventJcjh::getLng, bo.getLng());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), EventJcjh::getStatus, bo.getStatus());
        lqw.eq(bo.getSpr() != null, EventJcjh::getSpr, bo.getSpr());
        lqw.ge(bo.getStart() != null, EventJcjh::getCreateTime, bo.getStart());
        lqw.le(bo.getEnd() != null, EventJcjh::getCreateTime, bo.getEnd());
        return lqw;
    }

    /**
     * 新增检测计划
     */
    @Override
    public Boolean insertByBo(EventJcjhBo bo) {
        EventJcjh add = BeanUtil.toBean(bo, EventJcjh.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setJcjhId(add.getJcjhId());
        }
        return flag;
    }

    /**
     * 修改检测计划
     */
    @Override
    public Boolean updateByBo(EventJcjhBo bo) {
        EventJcjh update = BeanUtil.toBean(bo, EventJcjh.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EventJcjh entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除检测计划
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void renderEventInfo(Long userId, List<EventPlan> eventPlanList) {
        LambdaQueryWrapper<EventJcjh> lqw = Wrappers.lambdaQuery();
        lqw.eq(EventJcjh::getUserId, userId);
        lqw.orderByAsc(EventJcjh::getJcrq);
        List<EventJcjh> eventJcjhList = baseMapper.selectList(lqw);
        eventPlanList.forEach(eventPlan -> {
            eventJcjhList.forEach(eventJcjh -> {
                if (eventPlan.isOccurInThisPlanCycle(eventJcjh.getJcrq())) {
                    eventPlan.setEventId(eventJcjh.getJcjhId());
                    eventPlan.setEventDate(eventJcjh.getJcrq());
                }
            });
        });
    }
}
